const BASE_URL = 'https://localhost:7271/api';

export const loadCategoryById = (categoryId) => {
    return new Promise((resolve, reject) => {
        uni.request({
            url: `${BASE_URL}/Categories/${categoryId}`,
            method: 'GET',
            success: (res) => {
                if (res.statusCode === 200) {
                    resolve(res.data);
                } else {
                    reject(new Error('Failed to load category'));
                }
            },
            fail: (err) => {
                reject(err);
            }
        });
    });
};

export const loadQuestionByCategoryId = (categoryId) => {
    return new Promise((resolve, reject) => {
        uni.request({
            url: `${BASE_URL}/Categories/${categoryId}/questions`,
            method: 'GET',
            success: (res) => {
                if (res.statusCode === 200) {
                    resolve(res.data);
                } else {
                    reject(new Error('Failed to load category'));
                }
            },
            fail: (err) => {
                reject(err);
            }
        });
    });
};
export const loadCategories = () => {
    return new Promise((resolve, reject) => {
        uni.request({
            url: `${BASE_URL}/Categories`,
            method: 'GET',
            success: (res) => {
                if (res.statusCode === 200) {
                    resolve(res.data);
                } else {
                    reject(new Error('Failed to load category'));
                }
            },
            fail: (err) => {
                reject(err);
            }
        });
    });
};

export const getQuestionOptions = (categoryId, questionId) => {
    return new Promise((resolve, reject) => {
        uni.request({
            url: `${BASE_URL}/question/options/${categoryId}/${questionId}`,
            method: 'GET',
            success: (res) => {
                if (res.statusCode === 200) {
                    resolve(res.data);
                } else {
                    reject(new Error('Failed to load question options'));
                }
            },
            fail: (err) => {
                reject(err);
            }
        });
    });
};
export const getTestResult = (score, category) => {
  return new Promise((resolve, reject) => {
    uni.request({
      url: `${BASE_URL}/Result/getTestResult`,
      method: 'POST',
      data: {
        score: score,
        category: category
      },
      success: (res) => {
        if (res.statusCode === 200) {
          resolve(res.data);
        } else {
          reject(new Error('Failed to load test result'));
        }
      },
      fail: (err) => {
        reject(err);
      }
    });
  });
};
export const saveTestScore = (score, category) => {
  return new Promise((resolve, reject) => {
    uni.request({
      url: `${BASE_URL}/Result/saveTestScore`,
      method: 'POST',
      data: {
        score: score,
        category: category
      },
      success: (res) => {
        if (res.statusCode === 200) {
          resolve(res.data);
        } else {
          reject(new Error('Failed to save test score'));
        }
      },
      fail: (err) => {
        reject(err);
      }
    });
  });
};
export const getAllTestScores = () => {
    return new Promise((resolve, reject) => {
        uni.request({
            url: `${BASE_URL}/Result/getAllTestScores`,
            method: 'GET',
            success: (res) => {
                if (res.statusCode === 200) {
                    resolve(res.data);
                } else {
                    reject(new Error('Failed to load test scores'));
                }
            },
            fail: (err) => {
                reject(err);
            }
        });
    });
};
export const getTestScoresWithCategoryNames = () => {
    return new Promise((resolve, reject) => {
        uni.request({
            url: `${BASE_URL}/Result/getTestScoresWithCategoryNames`, // 使用新的端点
            method: 'GET',
            success: (res) => {
                if (res.statusCode === 200) {
                    resolve(res.data);
                } else {
                    reject(new Error('Failed to load test scores with category names'));
                }
            },
            fail: (err) => {
                reject(err);
            }
        });
    });
};
